<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\order\admin;

use app\common\builder\ZBuilder;
use app\admin\controller\Admin;
use app\order\model\Order as OrderModel;
use think\Db;

/**
 * 订单管理
 * @package app\admin\controller
 */
class Index extends Admin {

//    订单列表
    public function index() {
        $map = $this->getMap();
        $order = $this->getOrder('create_time desc');
        $data_list = OrderModel::where($map)->order($order)->paginate();
        p($data_list);die;
//        foreach ($data_list['data'] as &$v) {
//            $v['pay_type'] = $v['pay_type'] ? payStatus($v['pay_type']) : '未付款';
//            $v['status_t'] = $v['status'] ? orderStatus($v['status']) : '未付款';
//        }
        return ZBuilder::make('table')
                        ->setPageTitle('订单列表')
                        ->setTableName('order')
                        ->hideCheckbox()
//            ->setSearch(['address' => '地址']) // 设置搜索框
                        ->addColumns([// 批量添加数据列
                                ['id', '编号'],
                                ['ordersn', '订单号', 'text'],
                                ['create_time', '下单时间', 'datetime'],
                                ['openid', '用户', 'text'],
                                ['price', '订单金额', 'text.edit'],
                                ['actual_payment', '实际支付', 'text'],
                                ['freight', '运费', 'text'],
                                ['status_t', '订单状态', 'text','callback',function($value,$data){
                                    return $data['name'];
                                },'__data__'
                            ],
                                ['pay_type', '支付方式', 'text'],
                                ['right_button', '操作', 'btn']
                        ])
//            ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
                        ->addRightButtons(
                                ['edit',
                                    'delete' => ['data-tips' => '删除后无法恢复。'],
                                    'details' => ['title' => '详情',
                                        'icon' => 'fa fa-smile-o',
                                        'class' => 'btn btn-xs btn-default',
                                        'href' => url('details', ['id' => '__id__']),
                                        'target' => '_self'],
                        ]) // 批量添加右侧按钮
                        ->addColumn('nickname', '昵称')
                        ->replaceRightButton(['status' => ['in', '0']], '', ['edit', 'delete'])
                        ->setRowList($data_list) // 设置表格数据
                        ->fetch(); // 渲染模板
    }

//    订单详情
    public function details($id = null) {
        if ($id === null){
            return $this->error('缺少参数');
        }
        $order = Db::name('order')->where('id', $id)->find();
        $goods = Db::name('order_product')
                ->alias('p')
                ->field('g.id,g.name,g.originalPrice,g.minPrice,g.status,p.total, a.path')
                ->join('goods_goods g','g.id = p.productid')
                ->join('admin_attachment a','a.id = g.pics')
                ->where(array('orderid'=>$id))
                ->select();
        $order['pay_type'] = $order['pay_type'] ? payStatus($order['pay_type']) : '未付款';
        $order['status'] = $order['status'] ? orderStatus($order['status']) : '未付款';
        $address = unserialize($order['address']);
        $address['address'] = $address['province'].$address['city'].$address['district'].$address['address'];
        $this->assign('address',$address);
        $this->assign('order', $order);
        $this->assign('goods', $goods);
        $this->assign('page_title', '订单详情');
        return $this->fetch(); // 渲染模板
    }

//    订单修改
    public function edit($id = null) {
        
    }

//    删除订单
    public function delete($id = null) {
        
    }

}
